#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>

typedef struct _sig_info_ {
    int sig; // 记录接收到的信号值
    int index; // 记录发送时的次序
} SigInfo;

static int g_index = 0;
static SigInfo g_sig_arr[128] = {0};

static void signal_handler(int sig, siginfo_t* info, void* ucontext)
{
    g_sig_arr[g_index].sig = info->si_signo;
    g_sig_arr[g_index].index = info->si_value.sival_int;
    g_index++;
}

int main(int argc, char* argv[])
{
    struct sigaction act = {0};
    sigset_t set = {0};
    int i = 0;
    
    printf("current pid(%d) ...\n", getpid());

    act.sa_sigaction = signal_handler;
    act.sa_flags = SA_RESTART | SA_SIGINFO;

    for (i = 1; i <= 64; i++) {
        sigaddset(&act.sa_mask, i);
    }

    for (i = 1; i <= 64; i++) {
        sigaction(i, &act, NULL);
    }

    sigfillset(&set);
    sigprocmask(SIG_SETMASK, &set, NULL);

    for (i = 0; i < 15; i++) {
        printf("i = %d\t", i);
        sleep(1);
    }
    printf("\n");
    
    sigemptyset(&set);
    sigprocmask(SIG_SETMASK, &set, NULL);

    for (i = 0; i < g_index; i++) {
        printf("%d : %d\n", g_sig_arr[i].sig, g_sig_arr[i].index);
    }

    return 0;
}

